package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ：seanyang
 * @date ：Created in 2019/6/18
 * @description ：检查项控制器
 * @version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/checkitem")
public class CheckItemController {

	@Reference
	private CheckItemService checkItemService;

	/**
	 * 新增检查项
	 *  1.调用Service完成新增业务
	 *  2.成功，返回添加成功消息
	 *  3.失败，返回添加失败消息
	 * @param checkItem
	 * @return
	 */
	@RequestMapping("/add")
	public Result add(@RequestBody CheckItem checkItem){
		log.debug("add:{}",checkItem);
		try{
			// 调用Service完成新增业务
			if(checkItemService != null) {
				checkItemService.add(checkItem);
				// 成功，返回添加成功消息
				return new Result(true, MessageConst.ADD_CHECKITEM_SUCCESS);
			}else{
				log.debug("checkItemService is null ,未找到服务对象");
			}
		}catch(Exception e){
		    e.printStackTrace();
		}
		// 失败，返回添加失败消息
		return new Result(false, MessageConst.ADD_CHECKITEM_FAIL);
	}

	/**
	 * 分页查询
	 *  1.调用Service获取分页结果数据
	 *  2.成功，直接返回有内容的结果
	 *  3.失败，返回初始化结果，记录为0，数据为空
	 * @paramPageBean
	 * @return
	 */

	@RequestMapping("/findPage")
	public PageResult findPage(@RequestBody QueryPageBean pageBean){
			 try {
				 PageResult pageResult = checkItemService.pageQuery(pageBean);
				 return pageResult;
			 }catch (Exception e){
		             e.printStackTrace();
		         }
		         return new PageResult(0L,null);
	}

	@RequestMapping("/delete")
	public Result delete (Integer id){
			 try {
		             checkItemService.delete(id);
		             return new Result(true,MessageConst.DELETE_CHECKITEM_SUCCESS);
		         }catch (Exception e){
		             e.printStackTrace();
		             return new Result(false, e.getMessage());
		         }
		}
	@RequestMapping("/findById")
	public Result findById(Integer id){
			 try{
				 CheckItem checkItem = checkItemService.findById(id);
				 return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,checkItem);
			 }catch (Exception e){
		             e.printStackTrace();
		             return new Result(false,MessageConst.QUERY_CHECKITEM_FAIL);
		         }
	}
	@RequestMapping("/update")
	public Result update(@RequestBody CheckItem checkItem){
		 try {
		           checkItemService.update(checkItem);
		           return new Result(true,MessageConst.EDIT_CHECKITEM_SUCCESS);
		         }catch (Exception e){
		             e.printStackTrace();
		             return new Result(false,MessageConst.EDIT_CHECKITEM_FAIL);
		         }
	}
	@RequestMapping("/findAll")
	public Result findAll(){
		 try {
			 List<CheckItem> itemList = checkItemService.findAll();
			 return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,itemList);
		 }catch (Exception e){
		             e.printStackTrace();
		             return new Result(false,MessageConst.QUERY_CHECKITEM_FAIL);
		         }
	}
}
